package com.spring.Log;

import com.spring.Entity.AdminEntity;
import com.spring.Entity.UserEntity;
import com.spring.Service.Interface.AdminService;
import com.spring.Service.Interface.LogService;
import com.spring.Service.Interface.UserServie;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * Created by Arabira on 2016/8/15.
 */
@Aspect
@Component
public class PayLog {
    @Autowired
    private UserServie userServie;
    @Autowired
    private AdminService adminService;
    @Autowired
    private LogService Log;
    private String Domian = "交易记录";

    @Before("execution(* com.spring.Controller.PaylogControllerImpl.addPaylog(..))")
    public void add(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
        HttpSession session = (HttpSession) values[values.length - 2];
        String userId = (String) session.getAttribute("SID");
        UserEntity userEntity = userServie.findById(userId);
        Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "添加操作");
    }

    @Before("execution(* com.spring.Controller.PaylogControllerImpl.findPaylogByContractId(..))")
    public void find(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        HttpServletRequest request = (HttpServletRequest) values[2];
        HttpSession session = (HttpSession) values[1];
        String userId = (String) session.getAttribute("SID");
        String contractId = (String) values[0];
        UserEntity userEntity = userServie.findById(userId);
        if(userEntity == null){
            AdminEntity adminEntity = adminService.findById(userId);
            Log.log(adminEntity.getAdminName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "查找操作，查找记录中合同号为：" + contractId);
        }else {
            Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "查找操作，查找记录中合同号为：" + contractId);
        }
    }

    @Around("execution(* com.spring.Controller.PaylogControllerImpl.up*(..))")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] values = joinPoint.getArgs();
        Map response = (Map) joinPoint.proceed();
        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
        HttpSession session = (HttpSession) values[values.length - 2];
        String userId = (String) session.getAttribute("SID");
        UserEntity userEntity = userServie.findById(userId);
        if(userEntity == null){
            AdminEntity adminEntity = adminService.findById(userId);
            if (response.get("status").equals("ok")) {
                Log.log(adminEntity.getAdminName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "修改参数成功");
            } else {
                Log.log(adminEntity.getAdminName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "操作失败，错误为：" + response.get("description"));
            }
        }else {
            if (response.get("status").equals("ok")) {
                Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "修改参数成功");
            } else {
                Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "操作失败，错误为：" + response.get("description"));
            }
        }
        return response;

    }

}
